package com.zqq.goods.ui.activity

import android.os.Bundle
import android.support.design.widget.TabLayout
import android.view.Gravity
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.eightbitlab.rxbus.Bus
import com.eightbitlab.rxbus.registerInBus
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ui.activity.BaseActivity
import com.zqq.baselibrary.utils.AppPrefsUtils
import com.zqq.goods.R
import com.zqq.goods.event.AddCartEvent
import com.zqq.goods.event.UpdateCartSizeEvent
import com.zqq.goods.ui.adapter.GoodsDetailVpAdapter
import com.zqq.provider.common.afterLogin
import com.zqq.provider.router.RouterPath
import kotlinx.android.synthetic.main.activity_goods_detail.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import q.rorbin.badgeview.QBadgeView

/**
 * 商品详情（只是一个容器-视图在Fragment里） Activity(SKU)
 * wc
 */
@Route(path = RouterPath.GoodsCenter.PATH_GOODS_DETAIL)
class GoodsDetailActivity : BaseActivity() {

    private lateinit var mCartBdage: QBadgeView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_goods_detail)
        initView()
        initObserve()
        //刚进来时的显示购物车数量
        loadCartSize()
    }

    //FragmentTabOne里用到
    fun getRootView(): View {
        return mRootView
    }

    //初始化视图
    private fun initView() {
        mGoodsDetailVp.adapter = GoodsDetailVpAdapter(supportFragmentManager, this)
        //TabLayout关联ViewPager
        mGoodsDetailTab.tabMode = TabLayout.MODE_FIXED
        mGoodsDetailTab.setupWithViewPager(mGoodsDetailVp)

        EventBus.getDefault().register(this)
        //点击加入购物车（在GoodsSkuPopView也有一个按钮-加入购物车）
        mAddCartBtn.onClick {
            afterLogin {
                Bus.send(AddCartEvent())
                //发送事件-好像需要注册
//                EventBus.getDefault().post(AddCartEvent())
            }
        }

        //跳转到购物车界面
        //因为CartFragment只是一个Fragment，所以需要一个Activity来承载
        mEnterCartTv.onClick {
            ARouter.getInstance()
                    .build(RouterPath.GoodsCenter.PATH_CART)
                    .navigation()
        }

        mLeftIv.onClick { finish() }

        //初始化角标
        mCartBdage = QBadgeView(this)
    }

    /**
     *加载购物车数量
     */
    private fun loadCartSize() {
        setCartBadge()
    }

    /**
     * 监听购物车数量变化
     */
    private fun initObserve() {
        Bus.observe<UpdateCartSizeEvent>()
                .subscribe {
                    //添加角标（其实在MainActivity里也要设置）
                    setCartBadge()
                }.registerInBus(this)
    }

    //EventBus接收消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onUpdateCartSizeEvent(event: UpdateCartSizeEvent) {
        //添加角标
        setCartBadge()
    }

    //设置购物车标签（其实在MainActivity里也要设置）
    private fun setCartBadge() {
        //角标在那个位置
        mCartBdage.badgeGravity = Gravity.END or Gravity.TOP
        //设置外边距
        mCartBdage.setGravityOffset(22f, -2f, true)
        mCartBdage.setBadgeTextSize(8f, true)
        mCartBdage.bindTarget(mEnterCartTv)
                .badgeNumber = AppPrefsUtils.getInt(RouterPath.GoodsCenter.SP_CART_SIZE)
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        Bus.unregister(this)
    }

}
